﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;


namespace QuanLyQuanCafe
{
    public partial class FrmGhiNo : Form
    {
        public FrmGhiNo()
        {
            InitializeComponent();
        }
        KhachHangBL khachhangBL = new KhachHangBL();
        HoaDonBL hoadonBL = new HoaDonBL();
        BanBL banBL = new BanBL();
        string maban;
        string mahd;
        public string GetNoMoi
        {
            get { return this.tbNoMoi.Text;}
            set{this.tbNoMoi.Text = value;}
        }
        public string GetMaHD 
        {
            get { return mahd; }
            set { mahd = value; }
        }
        public string GetMaBan
        {
            get { return maban; }
            set { maban = value; }
        }

        private void FrmGhiNo_Load(object sender, EventArgs e)
        {
           
            cbKhachHang.DisplayMember = "HoTen";
            cbKhachHang.ValueMember = "CMND_KH";
            cbKhachHang.DataSource = khachhangBL.LayDanhSachKhachHang();
            //cbKhachHang.SelectedIndex = 0;
            tbTongTien.Text = (float.Parse(tbNoCu.Text)+float.Parse(tbNoMoi.Text)).ToString();
        }

        private void cbKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            string makh = cbKhachHang.SelectedValue.ToString();
            //MessageBox.Show(makh);
            DataTable dt = khachhangBL.LayDanhSachKhachHang(makh);
            if (dt.Rows.Count > 0)
            {
                tbNoCu.Text = dt.Rows[0]["TongNo"].ToString();
            }
            tbTongTien.Text = (float.Parse(tbNoCu.Text) + float.Parse(tbNoMoi.Text)).ToString();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            int tinhtrang;
            DialogResult result = MessageBox.Show("Bạn Có Chắc Muốn Thêm Nợ Mới Cho Khách Hàng ["+cbKhachHang.Text+"]?","Quản Lý Quán Café", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes) 
            {
                khachhangBL.UpTongNo(tbTongTien.Text, cbKhachHang.SelectedValue.ToString());
                tinhtrang = 1;
                hoadonBL.ThanhToanHD(tinhtrang.ToString(), mahd);
                tinhtrang = 3;
                banBL.SuaTinhTrang(tinhtrang.ToString(), maban);
                Close();
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
